package com.example.contadorcomida;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.Uri;
import android.preference.PreferenceManager;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;

public class ActivityBasic extends Activity{
	private static final int CANCELAR = 1;
	private static final int CyS = 2;

	public void entrarAWebActivity(){
		String web = "http://cys.com.ar/"; 
		Intent intent1 = new Intent(Intent.ACTION_VIEW); 
		intent1.setData(Uri.parse(web)); 
		startActivity(intent1); 
	}
	
	private android.content.DialogInterface.OnClickListener listenerLanzarPagWeb = new DialogInterface.OnClickListener() {
		@Override
		public void onClick(DialogInterface dialog, int which) {
			ActivityBasic.this.entrarAWebActivity(); 
		}
	};
	
	protected void crearPreferencias (String nombrePreferencia){
		SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this); 
		
		SharedPreferences.Editor editor = preferences.edit(); 
		
		editor.putString("acercaDe", nombrePreferencia); 
		editor.commit(); 
	}
	
	@Override
	protected Dialog onCreateDialog(int id) {
		AlertDialog.Builder builder = null; 
		switch (id) {
		case CANCELAR:
			builder= new AlertDialog.Builder(this);
			builder.setMessage(getResources().getString(R.string.preguntaTerminar)); 
			builder.setCancelable(true); 
			builder.setPositiveButton(getResources().getString(R.string.si), 
				new DialogInterface.OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog, int which) {
						ActivityBasic.this.finish(); 
					}
				} ); 
				builder.setNegativeButton(getResources().getString(R.string.no), 
					new DialogInterface.OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog, int which) {
						dialog.cancel(); 
					}
				}); 
			return builder.create(); 
		case CyS:
			builder = new AlertDialog.Builder(this); 
			//builder.setMessage(getResources().getString(R.string.cys)); 
			String mensaje = PreferenceManager.getDefaultSharedPreferences(this).getString("acercaDe", "Acerca de ..."); 
			
			builder.setMessage(mensaje);

			builder.setCancelable(true);
			//builder.setPositiveButton(R.string.irAWeb, listenerLanzarPagWeb ); 
			return builder.create();
		default:
			return super.onCreateDialog(id); 
		}
	};
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater inflater = getMenuInflater(); 
		inflater.inflate(R.menu.activity_main, menu); 

		return super.onCreateOptionsMenu(menu); 
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
			case R.id.menu_acerca_de:
				showDialog(CyS); 
				return true; 
			case R.id.menu_salir:
				showDialog(CANCELAR);
				return true; 
			case R.id.menu_configuracion:
				Intent intent = new Intent(this, ConfiguracionActivity.class); 
				startActivity(intent); 
				return true; 
			case R.id.menu_estadisticas:
				Intent intent1 = new Intent(this, EstadisticasActivity.class); 
				startActivity(intent1); 
				return true; 
			default:
				return super.onOptionsItemSelected(item);
		}
	}
}
